package com.pay.clickevent

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.provider.Settings
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.Toast
import com.pay.clickevent.R
import kotlinx.android.synthetic.main.activity_main.*
import android.os.StrictMode


class MainActivity : AppCompatActivity() {

    private var dialog: android.support.v7.app.AlertDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
        StrictMode.setThreadPolicy(policy)
        setContentView(R.layout.activity_main)
        btn_open.setOnClickListener {
            //打开支付宝
         //   job()
            openApp()


        }
    }


    private fun job() {


        val runnable = Runnable {
            while (true) {
                if (!Step.stasts && ClickService.jobNun > 300) {
                    Step.stasts = true
                    ClickService.jobNun = 0
                    openApp()
                }
                println("------------------" + ClickService.jobNun + "--" + Step.stasts)
                try {
                    Thread.sleep(1000)
                    ClickService.jobNun++
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }

            }
        }
        val thread = Thread(runnable)
        thread.start()


    }

    private fun openApp() {
        ClickService.jobNun = 0
        ClickService.account = "" //支付宝账号
        ClickService.step = Step.TOPersonInfo //操作步骤,默认到我的
        if (isApkInstalled(application, ClickService.targetPackageName)) {
            val intent = packageManager.getLaunchIntentForPackage(ClickService.targetPackageName)
            if (intent != null) {
                intent.action = "com.alipay.mobile.bill.list.ui.BillMainListActivity"
                startActivity(intent)
            }
        } else {
            Toast.makeText(this, "未安装支付宝App", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onResume() {
        super.onResume()

        if (null == HttpUtil.heartBeat("server-test")) {
            Toast.makeText(this, "服务器连接失败...", Toast.LENGTH_SHORT).show()
            btn_open.isEnabled = false
            btn_open.setText("服务器连接失败...")
            return
        }

        if (ClickService.isRunning) {
            dialog?.dismiss()
        } else {
            showOpenAccessibilityServiceDialog()
        }

    }

    /** 显示未开启辅助服务的对话框 */
    private fun showOpenAccessibilityServiceDialog() {
        if (dialog != null && dialog!!.isShowing) {
            return
        }
        val view = layoutInflater.inflate(R.layout.dialog_tips_layout, null)
        view.setOnClickListener(View.OnClickListener { openAccessibilityServiceSettings() })
        val builder = android.support.v7.app.AlertDialog.Builder(this)
        builder.setTitle("需要开启辅助服务正常使用")
        builder.setView(view)
        builder.setPositiveButton("打开辅助服务") { dialog, which -> openAccessibilityServiceSettings() }
        dialog = builder.show()
    }

    /** 打开辅助服务的设置 */
    private fun openAccessibilityServiceSettings() {
        try {
            val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
            startActivity(intent)
            Toast.makeText(this, "找到 [点击辅助] ,然后开启服务", Toast.LENGTH_LONG).show()
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    /**
     * 是否已安装
     */
    private fun isApkInstalled(context: Context, packageName: String): Boolean {
        if (TextUtils.isEmpty(packageName)) {
            return false
        }
        return try {
            val info = context.packageManager.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES)
            true
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            false
        }

    }

    companion object {


    }
}
